/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.PageHelper;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class BankAccountDetailDaoImpl extends BaseDaoImpl
		implements
			BankAccountDetailDao {
	public BigDecimal getCost(long bankAccountId, Date startDate, Date endDate) {
		String sql = "select sum(case when rc_type=\'R\' then cost else 0-cost end) from bank_account_detail where bank_account_id="
				+ bankAccountId
				+ " and bill_date>=:startDate"
				+ " and bill_date<:endDate" + " and log_data=\'N\'";
		Session sess = null;

		BigDecimal arg9;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			List list = q.list();
			if (!list.isEmpty() && list.get(0) != null) {
				arg9 = (BigDecimal) list.get(0);
				return arg9;
			}

			arg9 = new BigDecimal(0);
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg9;
	}

	public List getCost(String[] idArr, Date startDate, Date endDate,
			String financeType, String rcType, String listType) {
		String idStr = PageHelper.createIdArrStr(idArr, "bank_account_id",
				false);
		String sql = "select rc_type,sum(cost) as cost_total from bank_account_detail where "
				+ idStr;
		if (listType.equals("LOG")) {
			sql = sql + " and cost_date>=:startDate and cost_date<:endDate";
		} else {
			sql = sql
					+ " and bill_date>=:startDate and bill_date<:endDate and log_data=\'N\'";
		}

		if (financeType != null && !financeType.equals("")) {
			sql = sql + " and finance_type=:financeType";
		}

		if (rcType != null && !rcType.equals("")) {
			sql = sql + " and rc_type=:rcType";
		}

		sql = sql + " group by rc_type";
		Session sess = null;

		List arg11;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			if (financeType != null && !financeType.equals("")) {
				q.setString("financeType", financeType);
			}

			if (rcType != null && !rcType.equals("")) {
				q.setString("rcType", rcType);
			}

			arg11 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg11;
	}

	public List<?> getList(String[] idArr, Date startDate, Date endDate,
			String financeType, String rcType, String listType) {
		String idStr = PageHelper.createIdArrStr(idArr, "d.bankAccountId",
				false);
		String sql = "from com.minierp.model.BankAccountDetail d where "
				+ idStr;
		if (listType.equals("LOG")) {
			sql = sql + " and d.costDate>=:startDate and d.costDate<:endDate";
		} else {
			sql = sql
					+ " and d.billDate>=:startDate and d.billDate<:endDate and d.logData=\'N\'";
		}

		if (financeType != null && !financeType.equals("")) {
			sql = sql + " and d.financeType=:financeType";
		}

		if (rcType != null && !rcType.equals("")) {
			sql = sql + " and d.rcType=:rcType";
		}

		if (listType.equals("LOG")) {
			sql = sql + " order by d.id";
		} else {
			sql = sql + " order by d.billDate,d.id";
		}

		Session sess = null;

		List arg11;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			if (financeType != null && !financeType.equals("")) {
				q.setString("financeType", financeType);
			}

			if (rcType != null && !rcType.equals("")) {
				q.setString("rcType", rcType);
			}

			arg11 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg11;
	}

	public List<?> getBankAccountCost(Date startDate, Date endDate) {
		String sql = "select bank_account_id,finance_type,sum(cost) from bank_account_detail where bill_date>=:startDate and bill_date<:endDate and log_data=\'N\' group by bank_account_id,finance_type";
		Session sess = null;

		List arg6;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			q.setDate("startDate", startDate);
			q.setDate("endDate", endDate);
			arg6 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg6;
	}

	public int updateLogData(final String billType, final long billId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.BankAccountDetail set logData=\'Y\' where billType=:billType and billId=:billId";
						Query q = session.createQuery(hql);
						q.setString("billType", billType);
						q.setLong("billId", billId);
						return new Integer(q.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List getAppDetaCoun(long baId, Date startDate, Date endDate) {
		String sql = "select rc_type,count(*),sum(cost) as cost_total from bank_account_detail where bank_account_id="
				+ baId + " and log_data=\'N\'";
		if (startDate != null) {
			sql = sql + " and bill_date>=:startDate";
		}

		if (endDate != null) {
			sql = sql + " and bill_date<:endDate";
		}

		sql = sql + " group by rc_type";
		Session sess = null;

		List arg8;
		try {
			sess = this.getSession();
			SQLQuery q = sess.createSQLQuery(sql);
			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			arg8 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg8;
	}

	public List getAppDetaList(long baId, Date startDate, Date endDate,
			int startIndex, int num) {
		String sql = "from com.minierp.model.BankAccountDetail d where d.bankAccountId="
				+ baId + " and d.logData=\'N\'";
		if (startDate != null) {
			sql = sql + " and d.billDate>=:startDate";
		}

		if (endDate != null) {
			sql = sql + " and d.billDate<:endDate";
		}

		sql = sql + " order by d.billDate,d.id";
		Session sess = null;

		List arg10;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (startDate != null) {
				q.setDate("startDate", startDate);
			}

			if (endDate != null) {
				q.setDate("endDate", endDate);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg10 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg10;
	}
}